package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 杨林
 * @create 2022-09-13 15:57 星期二
 * description:
 */
@Controller
public class IndexController {

    @Reference
    private PermissionService permissionService;

    @Reference
    private AdminService adminService;

    @RequestMapping("/")
    public String index(Model model){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User principal = (User)authentication.getPrincipal();
        String username = principal.getUsername();
        Admin admin = adminService.getByUsername(username);
        //先将用户id设置为1 之后通过登录功能替换用户
        //动态获取用户id
        Long adminId = admin.getId();


        //根据用户名id获取用户
        //Admin admin = adminService.getById(adminId);
        model.addAttribute("admin",admin);

        List<Permission> permissionList = permissionService.findMenuPermissionByAdminId(adminId);
        model.addAttribute("permissionList",permissionList);
        return "frame/index";
    }

    @RequestMapping("/main")
    public String main(){

        return "frame/main";
    }

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "frame/login";
    }

    /**
     * 当用户没有权限时  跳转到没有权限的提示页面
     */
    @RequestMapping("/auth")
    public String auth(){
        return  "frame/auth";
    }
}
